//
//  ChainSerialTaskItem.swift
//  
//
//  Created by tangbl93 on 2021/3/3.
//

import Foundation
import Promises

// MARK: ChainSerialTaskItem
public struct ChainSerialTaskItem {

    public var key: String
    
    var promise: Promise<Void>
    
    fileprivate init(key: String, promise: Promise<Void>) {
        self.key = key
        self.promise = promise
    }
    
    public func fulfill() {
        promise.fulfill(())
    }
    
    public func reject(_ error: Error = InternError.unknow) {
        promise.reject(error)
    }
}

public extension ChainSerialTaskItem {
    
    // Error
    enum InternError: Error {
        case unknow
    }
    
    // Action
    typealias Action = (ChainSerialTaskItem) -> Void
}

public extension ChainSerialTaskItem {
    
    static func pending(key: String) -> Self {
        return Self(key: key, promise: Promise.pending())
    }
}


